<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <!-- 引入axios -->
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
    <!-- 引入Vue -->
    <script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
    <!-- import Element UI -->
    <script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
    <link rel="stylesheet" href="css/index.css">
    <script src="../echarts/echarts.min.js"></script>
</head>
<body>
<div id="app">
    <el-container>
        <el-header>
            <h1>
                <img src="../imgs/shark.png">
                CoolShark商城管理系统
                <span v-if="" style="float: right;font-size: 15px;">
                    <a href="JavaScript:void(0)" style="text-decoration: none ;font-family: 楷体"
                       @click="logout()">退出登录</a>
                </span>
            </h1>
        </el-header>
        <el-container>
            <el-aside>
                <!-- :unique-opened="true":设置子菜单只允许打开一个--->
                <el-menu
                        default-active="1"
                        class="el-menu-vertical-demo"
                        :unique-opened="true"
                        @select="handleSelect"
                        @open="handleOpen"
                        @close="handleClose">
                    <el-menu-item index="1">
                        <i class="el-icon-s-home"></i>
                        <span slot="title">首页</span>
                    </el-menu-item>
                    <el-submenu index="2">
                        <template slot="title">
                            <i class="el-icon-user-solid"></i>
                            <span>管理员管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="2-1" v-if="username==='root'">添加管理员</el-menu-item>
                            <el-menu-item index="2-2">管理员列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-submenu index="3">
                        <template slot="title">
                            <i class="el-icon-s-claim"></i>
                            <span>类别管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="3-1">添加类别</el-menu-item>
                            <el-menu-item index="3-2">类别列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-submenu index="4">
                        <template slot="title">
                            <i class="el-icon-s-order"></i>
                            <span>商品管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="4-1">添加商品</el-menu-item>
                            <el-menu-item index="4-2">商品列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-submenu index="5">
                        <template slot="title">
                            <i class="el-icon-picture"></i>
                            <span>轮播图管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="5-1">添加轮播图</el-menu-item>
                            <el-menu-item index="5-2">轮播图列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-menu-item index="6">
                        <i class = "el-icon-user-solid"></i>
                        <span>个人信息</span>
                    </el-menu-item>
                </el-menu>
            </el-aside>
            <el-main>
                <el-row span="4">
                    <el-col span="4">
                        <el-card>
                            <i class="el-icon-goods"> 商品总数</i>
                            <p style="margin: 0 auto"><b>{{goodsNum}}</b></p>
                        </el-card>
                    </el-col>
                    <el-col span="4" style="margin: 0 20px">
                        <el-card>
                            <i class="el-icon-user"> 用户总数</i>
                            <p style="margin: 0 auto"><b>{{userNum}}</b></p>
                        </el-card>
                    </el-col>
                </el-row>
                <el-row span="10" style="margin: 20px 0">
                    <el-col span="8">
                        <el-card style="height: 400px ;width: 400px">
                            <div id="cake" style="width: 400px;height: 400px"></div>
                        </el-card>
                    </el-col>
                    <el-col span="15" style="margin-left: 40px">
                        <el-card style="height: 400px ;width: 700px">
                            <div id="clumnar" style="width: 700px;height: 390px"></div>
                        </el-card>
                    </el-col>
                </el-row>
                <el-row span="10">

                </el-row>
            </el-main>
        </el-container>
    </el-container>
</div>
</body>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                username: localStorage.getItem('username'),
                goodsNum: '',
                userNum: ''
            }
        },
        methods: {
            /**
             * 根据不同的key值，重定向到相应的页面。
             * key: 用于识别要跳转到哪个页面的标识符。
             * 无返回值。
             */
            handleSelect(key, keyPath) {
                console.log(v.username);
                if (key === '2-1') {
                    location.href = 'AddAdmin.html'; // 跳转到添加管理员页面
                } else if (key === '2-2') {
                    location.href = 'AdminList.html'; // 跳转到管理员列表页面
                } else if (key === '3-1') {
                    let name = prompt("请输入类别名称：", "");
                    if (name === null) {
                        return;
                    }
                    if (name.trim() === '') {
                        v.$message({
                            message: '类别名称不能为空！',
                            type: 'warning'
                        });
                    }
                    axios.get('/carousel/insert', {params: {name: name}})
                        .then(function (response) {
                            v.carousel = response.data.data;
                            location.href = 'CarouselList.html'
                        });
                } else if (key === '3-2') {
                    location.href = 'CarouselList.html'; // 跳转到类别列表页面
                } else if (key === '4-1') {
                    location.href = 'AddGoods.html'
                } else if (key === '4-2') {
                    location.href = 'GoodsList.html'
                } else if (key === '5-1') {
                    location.href = 'AddBanner.html'
                } else if (key === '5-2') {
                    location.href = 'BannerList.html'
                }else if (key === '6') {
                    location.href = 'PersonalInfo.html'
                }
            },
            handleOpen(key, keyPath) {
                console.log('key = ' + key, 'leyPath = ' + keyPath);
            },
            handleClose(key, keyPath) {
                console.log('key = ' + key, 'leyPath = ' + keyPath);
            },
            logout() {
                localStorage.removeItem('username');
                location.href = '../login.html';
            }
        },
        mounted() {
            // 使用axios发起GET请求获取产品类别信息
            axios.get('/admin/getProductCategory')
                .then(function (response) {
                    let responseBody = response.data;
                    // 当响应状态码为20000时，处理数据并渲染图表name
                    if (responseBody.state === 20000) {
                        // 初始化ECharts图表
                        var myChart = echarts.init(document.getElementById('cake'));
                        // 配置图表选项
                        var option = {
                            title: {
                                text: '类别商品数量统计',
                                subtext: 'Product category statistics',
                                left: 'center'
                            },
                            tooltip: {
                                trigger: 'item',
                            },
                            series: [
                                {
                                    name: 'Access From',
                                    type: 'pie',
                                    radius: '50%',
                                    data: response.data.data, // 设置数据
                                    emphasis: {
                                        itemStyle: {
                                            shadowBlur: 10,
                                            shadowOffsetX: 0,
                                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                                        }
                                    }
                                }]
                        };
                        // 渲染图表
                        myChart.setOption(option);
                        console.log(responseBody.data)
                    }
                });
            // 获取每日新增上商品数量
            axios.get('/goods/getAddGoodsNum')
                .then(function (response) {
                    let responseBody = response.data;
                    if (responseBody.state === 20000) {
                        console.log(responseBody.data);
                        console.log("time = "+responseBody.data.time)
                        console.log("数量 = "+responseBody.data.num);
                        var myChart = echarts.init(document.getElementById('clumnar'));
                        var option = {
                            title: {
                                text: '每日新增商品数量统计',
                                subtext: 'Daily statistics of newly added goods',
                                left: 'center'
                            },
                            tooltip: {
                                trigger: 'axis',
                                axisPointer: {
                                    type: 'shadow'
                                }
                            },
                            grid: {
                                left: '3%',
                                right: '4%',
                                bottom: '3%',
                                containLabel: true
                            },
                            dataset: [{
                                source: responseBody.data,
                            }],
                            xAxis: [{
                                type: 'category',
                            }],
                            yAxis: [{
                                type: 'value',
                                name: '数量',
                                min: 0,
                                max: 50,
                                interval: 10,
                                axisLabel: {
                                    formatter: '{value} 个'
                                }
                            }],
                            series: [{
                                type: 'bar',
                                encode: {
                                    x: 'time',
                                    y: 'value'
                                }
                            }],
                        }
                        myChart.setOption(option);
                    }
                });
            // 获取商品总数
            axios.get('/admin/getGoodsNum')
                .then(function (response) {
                    v.goodsNum = response.data.data; // 更新商品总数
                });
            // 获取用户总数
            axios.get('/user/getUserNum')
                .then(function (response) {
                    v.userNum = response.data.data; // 更新用户总数
                });

        }
    })
</script>
</html>